﻿Public Class frmUserStoriesInsert

    Dim strNameForm As String = "Insert User Story"
    Public mId As Integer

    Public Function Initialize() As Boolean
        Try
            Me.Text = strNameForm

            cboSprints.Load("Select * From Sprints", "Name", gConnectionStringAgenda.ToString)
            cboProjects.Load("Select * From Projects", "Name", gConnectionStringAgenda.ToString)
            Dim listStatus As New List(Of Base.GlobalConst)
            listStatus.Add(US_STATUS_PLANNED)
            listStatus.Add(US_STATUS_ONGOING)
            listStatus.Add(US_STATUS_ONHOLD)
            listStatus.Add(US_STATUS_DONE)
            listStatus.Add(US_STATUS_CANCELLED)
            listStatus.Add(US_STATUS_NEXT)
            cboStatus.DataSource = listStatus
            cboStatus.ValueMember = "NUMBER"
            cboStatus.DisplayMember = "NAME"
            listStatus = Nothing

            AddHandler cboSprints.OnInsert, AddressOf InterfaceManager.Sprints_OnInsert
            AddHandler cboSprints.OnModify, AddressOf InterfaceManager.Sprints_OnModify
            AddHandler cboProjects.OnInsert, AddressOf InterfaceManager.Projects_OnInsert
            AddHandler cboProjects.OnModify, AddressOf InterfaceManager.Projects_OnModify

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "Commands"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInsert.Click
        Try
            If IsComplete() Then
                Dim oTUserStories As New Data.TUserStories
                oTUserStories.mName = txtName.Text
                oTUserStories.mNumber = txtNumber.text
                oTUserStories.mDescription = txtDescription.Text
                oTUserStories.mDone = txtDone.text
                oTUserStories.mPriority = txtPriority.GetIntNumber
                oTUserStories.mProjectsID = cboProjects.SelectedValue
                oTUserStories.mSprintsID = cboSprints.SelectedValue
                oTUserStories.mStatus = cboStatus.SelectedValue

                mId = oTUserStories.Insert
                If mId > 0 Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                Else
                    MsgBox("Impossible insert the user story.", vbExclamation, Me.Text)
                    Me.DialogResult = Windows.Forms.DialogResult.Cancel
                End If
                oTUserStories = Nothing
            End If
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "cmdInsert_Click", ex)
        End Try
    End Sub

#End Region

#Region "Other functions"

    Private Function IsComplete() As Boolean
        Try
            If txtNumber.Text = "" Then
                MsgBox("Insert number.", vbExclamation, Me.Name)
                txtDescription.Focus()
                Return False
            End If
            If Not cboSprints.IsItemSelected Then
                MsgBox("Select sprint.", vbExclamation, Me.Name)
                cboSprints.Focus()
                Return False
            End If

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "IsComplete", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Form events"

    Private Sub me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class